package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Encheres.Model.Produit;

public class ProduitDAO extends DAO<Produit> {

	public Produit create(Produit obj) {
		try {
			int id = getNextId();
			PreparedStatement prepare = this.connect
					.prepareStatement("INSERT INTO PRODUIT (PRO_LIBELLE, " + 
							"PRO_DESCRIPTION, "
							+ "PRO_UTI_ID, " + "PRO_CAT_ID, "
							+ "PRO_DATEPUBLICATION, PRO_DATE_FIN_ENCHERE, "
							+ "PRO_PRIX_DEPART"
							+ ") VALUES(?, ?, ?, ?, ?, ?, ?)");
			prepare.setString(1, obj.getTitre());
			prepare.setString(2, obj.getDescription());
			prepare.setInt(3, obj.getIdUser());
			prepare.setInt(4, obj.getIdCategorie());
			prepare.setString(5, obj.getDatePublication());
			prepare.setString(6, obj.getDateFinEnchere());
			prepare.setFloat(7, obj.getPrixDepart());

			prepare.executeUpdate();
			obj = this.find(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	public Produit find(int id) {
		Produit prod = new Produit();
		try {
			ResultSet result = this.connect.createStatement().executeQuery(
					"SELECT * FROM PRODUIT WHERE PRO_ID = " + id);
			if (result.next())
				prod = new Produit(id, result.getString("PRO_LIBELLE"), result
						.getString("PRO_DESCRIPTION"), result
						.getFloat("PRO_PRIX_DEPART"), result
						.getInt("PRO_CAT_ID"), result.getInt("PRO_UTI_ID"),
						result.getString("PRO_DATEPUBLICATION"), result
								.getString("PRO_DATE_FIN_ENCHERE"));

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return prod;
	}

	public Produit update(Produit obj) {
		try {

			this.connect.createStatement().executeUpdate(
					"UPDATE PRODUIT SET " + "PRO_LIBELLE = '" + obj.getTitre()
							+ "', " + "PRO_DESCRIPTION = '"
							+ obj.getDescription() + "', " + "PRO_UTI_ID = "
							+ obj.getIdUser() + ", " + "PRO_CAT_ID = "
							+ obj.getIdCategorie() + ", "
							+ "PRO_DATEPUBLICATION = '"
							+ obj.getDatePublication() + "', "
							+ "PRO_PRIX_DEPART = " + obj.getPrixDepart() +", "
							+ "PRO_DATE_FIN_ENCHERE = " +obj.getDateFinEnchere()
							+ "" + " WHERE PRO_ID = " + obj.getIdProduit());

			obj = this.find(obj.getIdProduit());
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return obj;
	}

	public void delete(Produit obj) {
		try {

			this.connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE).executeUpdate(
					"DELETE FROM PRODUIT WHERE PRO_ID = " + obj.getIdProduit());

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<Produit> selectAll()
	{
		ArrayList<Produit> listProd = new ArrayList<Produit>();
		try {
			Statement st = this.connect.createStatement();
			ResultSet result = st.executeQuery("SELECT * FROM PRODUIT");
			
			while (result.next())
			{
				Produit prod = new Produit(
						result.getInt("PRO_ID"), 
						result.getString("PRO_LIBELLE"), 
						result.getString("PRO_DESCRIPTION"), 
						result.getFloat("PRO_PRIX_DEPART"), 
						result.getInt("PRO_CAT_ID"), 
						result.getInt("PRO_UTI_ID"),
						result.getString("PRO_DATEPUBLICATION"), 
						result.getString("PRO_DATE_FIN_ENCHERE"));
				listProd.add(prod);
			}

		} catch (SQLException e) {
			System.out.println("ERREUR <ProduitDAO>.selectAll()");
			e.printStackTrace();
		}
		return listProd;
	}
	
	public int getNextId()
	{
		try
		{
			ResultSet result = this.connect.createStatement().executeQuery(
				"SELECT MAX(PRO_ID) as id FROM PRODUIT");
			if (result.next())
				return result.getInt("id");
			else
				return 1;
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			return 1;
		}
	}

}
